<?php

class Customer_CustomersController extends Zend_Controller_Action
{
	protected $_redirector = null;
	private $_session;
	
    public function init()
    {
//   	$this->_redirector = $this->_helper->getHelper('Redirector'); 
//		$tr = new Zend_Mail_Transport_Smtp('smtp.mail.yahoo.com');
//		Zend_Mail::setDefaultTransport($tr);
    }
    
    private function _getCustomerNS()
    {
		if($this->_session == null) {
			$this->_session = new Zend_Session_Namespace('Customer');
		}
		return $this->_session;
    }
    
	public function enterInfoAction()
	{		
		$customerForm = new Customer_Form_Customer();
		$customerForm->setAction('/customers/enter-info')->setMethod('post');
		$fromTicketList = $this->_request->getParam('fromTicketList');
		
		// get params from ticket/list
		$tripID = $this->_request->getParam('trip');
		$seatCountYouWant = $this->_request->getParam('seatCountYouWant');	
		$seats = $this->_request->getParam('seats');
		$tempSeats = $this->_request->getParam('tempSeats');
		// get selected seat number for later use
		if($seats)
			$selectedSeatNumbers = $this->_getSelectedSeatNumbers($seats);
				
		// check if the request is from ticket/list
		if($fromTicketList) {						
			// store them in session for reviewing later
			$this->_getCustomerNS()->tripID = $tripID;
			$this->_getCustomerNS()->seats = $seats;
			$this->_getCustomerNS()->tempSeats = $tempSeats;
			$this->_getCustomerNS()->seatCountYouWant = $seatCountYouWant;
			$this->_getCustomerNS()->selectedSeatNumbers = $selectedSeatNumbers;			
						
			// check if the user logged has customer role, if yes, he will be route to other page			
			if(($identity = TBB_Utility_Checker::isLoggedInUser()) && 
			   ($customerID = TBB_Utility_Checker::isCustomer($identity))) {			   		
					$this->_getCustomerNS()->customerID = $customerID;					
					// forward to orders/review
					$this->_request->setParam('fromCustomerEnterInfo', true);
					return $this->_forward('review', 'orders', 'customer');				
			}									
		} else {			
			if($this->_request->isPost()) {		
				// check if POST array is valid
				if($customerForm->isValid($_POST)) {				
					$this->_getCustomerNS()->customerData = $customerForm->getValues();					
					// forward to orders/review
					$this->_request->setParam('fromCustomerEnterInfo', true);
					return $this->_forward('review', 'orders', 'customer');						
				}
			}
		}
		
		// populate form
		$customerForm->getElement('trip')->setValue($tripID);
		$customerForm->getElement('seatCountYouWant')->setValue($seatCountYouWant);
		
		// store seats for later sending
		if($seats)
			$customerForm->addSeats($seats);
		
		// store temp seats for later sending
		if($tempSeats)
			$customerForm->addTempSeats($tempSeats);
			
		$this->view->form = $customerForm;
	}
	
	//////////////////////// Helpers ///////////////////////
	private function _getSelectedSeatNumbers($seats)
	{
		$selectedSeatNumbers = array();
		foreach($seats as $seatNumber => $isBooked) {
			if(substr($isBooked, 0, 3) == 'yes') {
				$selectedSeatNumbers[] = $seatNumber;
			}
		}		
		return $selectedSeatNumbers;
	}	
	
	/////////////// (deprecated) ///////////////
	private function createCode($length) {
		$chars = "1234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$i = 0;
		$password = "";
		while ($i <= $length) {
			$password .= $chars{mt_rand(0,strlen($chars) - 1)};
			$i++;
		}
		return $password;
	}

}

